<?php
namespace Client\Controller;
use Org\Net\Http;

use Think\Image;

use Think\Page;

use Think\Controller;
class AccountController extends Controller {
	public function wxRegister(){
		if(IS_POST){
			file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$data['wx_id'] = $_POST['unionid'];
			$data['password'] = md5('123456');
			$data['nickname'] = $_POST['nickname'];
			$data['age'] = $_POST['age'];
			$util = A('Util');
			$data['id'] = 'A'.$util->getId();//生成用户ID

			$uploadpath = "Public/Upload/headImage/";//上传路径
			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建

			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");

			$fileName = md5($name.time()).$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->save($uploadpath.$fileName);
			//				file_put_contents("Public/Log/log.txt", "image:".$file['name']."\r\n",FILE_APPEND|LOCK_EX);

			$data["image"] = $uploadpath.$fileName;


			$result = $account->save($data);
			if($result){
				$json['result'] = 'E0009';
				$json['msg'] = '新用户需完善资料';
				$json['account'] = $result;
			}else{
				$json['result'] = 'E0010';
				$json['msg'] = '微信登录失败';
			}
		}
	}
	public function signature(){
		if(IS_POST){
//			file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$account = D('Account');
			$data['signature'] = $_POST['signature'];
			$where['id'] = $_POST['account_id'];
			$result = $account->where($where)->save($data);
			if($result){
				$json['result'] = '0';
				$json['msg'] = '修改成功';
			}else{
				$json['result'] = '-1';
				$json['msg'] = '修改失败';
			}
			$this->ajaxReturn($json);
		}
	}
	public function getAccountById(){
		if(IS_POST){
			file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$account = D('Account');
			$where['id'] = $_POST['account_id'];
			$arr = $account->where($where)->field("id,nickname,headimgurl")->find();
			if($arr){
				$json['result'] = '0';
				$json['msg'] = '成功';
				$json['account'] = $arr;
			}else{
				$json['result'] = '-1';
				$json['msg'] = '无此用户信息';
			}
			file_put_contents("Public/Log/log.txt", "json:".json_encode($json)."\r\n",FILE_APPEND|LOCK_EX);
			$this->ajaxReturn($json);
		}
	}
	public function wxLogin(){
//		file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
		if(IS_POST){
			$account = D('Account');
			$where['wx_id'] = $_POST['unionid'];
			$result = $account->where($where)->find();
			
			vendor("rongcloud.ServerAPI");
			$api = new \ServerAPI();
				
			if($result){
				$json['result'] = '0';
				$json['msg'] = '登录成功';
				
				
				$back =  json_decode($api->getToken($result['id'], $result['nickname'], $result['headimgurl']),'json');
				$result['token'] = $back['token'];
				$json['account'] = $result;
			}else{
				$util = A('Util');
				$data['id'] = 'A'.$util->getId();//生成用户ID
				$data['wx_id'] = $_POST['unionid'];
				$data['sex'] = $_POST['sex'];
				$data['nickname'] = $_POST['nickname'];
				$data['headimgurl'] = $_POST['headimgurl'];
				$result = $account->add($data);
				$json['result'] = 'E0009';
				$json['msg'] = '新用户需完善资料';
				
				$back =  json_decode($api->getToken($data['id'], $data['nickname'], $data['headimgurl']),'json');
				$data['token'] = $back['token'];
				$data['birthday'] = "1990-01-01";
				$data['status'] = "0";
				$json['account'] = $data;
			}
			$this->ajaxReturn($json);
		}
	}
	public function index(){
		
		$account = D("Account");

		$count = $account->count();
		
		$page = new Page($count,4);
		$page->setConfig('prev', '上页');
		$page->setConfig('next', '下页');
		$show = $page->show();
		$list = $account->relation(true)->limit($page->firstRow,$page->listRows)->select();

		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$show);
		$this->display();
	}
	public function register(){
		if(IS_POST){
			$account = D("Account");
			$data = $account->create($_POST);
			if($data){
				$result = $account->add($data);
				if($result){
					$json['result'] = "0";
					$json['msg'] = "注册成功";
					
					vendor("rongcloud.ServerAPI");
					$api = new \ServerAPI();
					$back =  json_decode($api->getToken($data['id'], $data['nickname'], $data['headimgurl']),'json');
					
					$data['token'] = $back['token'];
					$json['account'] = $data;
				}else{
					$json['result'] = "E0001";
					$json['msg'] = "注册失败";
				}
			}else{
				$json['result'] = "E0001";
				$json['msg'] = $account->getError();
			}
			$this->ajaxReturn($json);
		}
	}
	public function login() {
		if(IS_POST){
			$where['phone'] = $_POST['phone'];
			$account = D('Account');
			$arr = $account->where($where)->find();
			if($arr){
				if($arr['password'] == md5($_POST['password'])){
					$json['result'] = "0";
					
					vendor("rongcloud.ServerAPI");
					$api = new \ServerAPI();
					$back =  json_decode($api->getToken($arr['id'], $arr['nickname'], $arr['headimgurl']),'json');
					$arr['token'] = $back['token'];
					
					$json['account'] = $arr;
					$json['msg'] = "登录成功";
				}else{
					$json['result'] = "E0003";
					$json['msg'] = "密码错误";
				}
			}else{
				$json['result'] = "E0002";
				$json['err'] = "账号不存在";
			}
			$this->ajaxReturn($json);
		}
	}
	public function resetPassword(){
		if(IS_POST){
			$old = $_POST['oldPwd'];
			$userid= $_POST['id'];
			$new  = $_POST['newPwd'];
			$account = D('Account');
			if(md5($old)==$account->where(array('id'=>$userid))->getField('password')){
				$result=$account->where(array('id'=>$userid))->setField('password',md5($new));
				if($result){
					$json['msg']="密码已经成功修改！";
					$json['result']="0";
					$this->ajaxReturn($json);
				}else{
					$json['msg']="密码修改失败！";
					$json['result']="-2";
					$this->ajaxReturn($json);
				}
			}else{
				$json['msg']="旧密码错误，无法修改！";
				$json['result']="-3";
				$this->ajaxReturn($json);
			}
		}else{
			$json['msg']="数据不是以post方式提交";
			$json['result']="-1";
			$this->ajaxReturn($json);
		}
	}
	public function completeInfo(){
		if(IS_POST){
			file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$file = $_FILES['pic'];
			file_put_contents("Public/Log/log.txt", "file:".json_encode($_FILES)."\r\n",FILE_APPEND|LOCK_EX);
			if($_POST['nickname']!=null)
			$data['nickname'] = $_POST['nickname'];
			if($_POST['birthday']!=null)
			$data['birthday'] = $_POST['birthday'];
			if($_POST['sex']!=null)
			$data['sex']	  = $_POST['sex'];
			if(sizeof($file)>0){
				$uploadpath = "Public/Upload/headImage/";//上传路径
				
				$util = A('Util');
				$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建
				
				$pinfo = pathinfo($file['name']);
				$ftype = $pinfo['extension'];//文件类型
				$name  = basename($file['name'],".{$ftype}");

				$fileName = md5($name.time()).'.'.$ftype;//识别图
				$image = new Image();
				$image_url = "http://qianmosen.cn/tubu/".$uploadpath.$fileName;
				$image->open($file['tmp_name']);
				if(is_file($image_url)){
					unlink($image_url);
				}
				if($file['size']<1048576){
					$image->save($uploadpath.$fileName);
				}
				else{
					$image->thumb(130, 130)->save($uploadpath.$fileName);
				}
				$data['headimgurl'] = $image_url;
			}
			$account = D('Account');
			if($account->create($data,2)){
				
				$data['status']='1';
			    
				if($account->where(array('id'=>$_POST['id']))->save($data)){
					$userInfo = $account->where(array('id'=>$_POST['id']))->find();
					$json['result']="0";
					$json['msg']="成功";
					$json['userInfo']=$userInfo;
					$this->ajaxReturn($json);
				}
				else{
					$json['result']="-2";
					$json['msg']="信息未修改，保存失败";
					$this->ajaxReturn($json);
				}
			}
			else{
				$this->ajaxReturn(array('result'=>-1,'msg'=>$account->getError()));
			}
		}
	}
}